package com.runfast.waimai.web.controller.business;

import com.runfast.common.web.entity.Result;
import com.runfast.waimai.dao.model.RunfastBusiness;
import com.runfast.waimai.dao.model.RunfastBusinessExample;
import com.runfast.waimai.dao.model.RunfastBusinessType;
import com.runfast.waimai.dao.model.RunfastBusinessTypeExample;
import com.runfast.waimai.service.RunfastBusinessService;
import com.runfast.waimai.service.RunfastBusinessTypeService;
import com.runfast.waimai.utils.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * ${DESCRIPTION}商家类型
 *
 * @author chenzheng
 * @create 2018-05-08 16:31
 **/
@RestController("businessTypeController")
@RequestMapping("/server/businessType")
public class BusinessTypeController {
    @Autowired
    private RunfastBusinessTypeService runfastBusinessTypeService;
    @Autowired
    private RunfastBusinessService runfastBusinessService;

    /**
     * 商家类型列表带分页的
     */
    @GetMapping("/list")
    //@RequiresPermissions("server:businessType:list")
    public Result list(@PageableDefault Pageable pageable, Map<String, Object> params){

        RunfastBusinessTypeExample example=new RunfastBusinessTypeExample();
        example.setOrderByClause("createtime  desc");
        example.createCriteria().andParentIdEqualTo(0);
        List<RunfastBusinessType> list=runfastBusinessTypeService.selectByExampleWithPageable(example,pageable);
        if(list.size()>0){
            for(RunfastBusinessType type:list){
                RunfastBusinessTypeExample example1=new RunfastBusinessTypeExample();
                example1.createCriteria().andParentIdEqualTo(type.getId());
                List<RunfastBusinessType> list1=runfastBusinessTypeService.selectByExample(example1);
                type.setSubList(list1);
            }
        }
        Integer total=runfastBusinessTypeService.countByExample(example);
        Map<Object,Object> map=new HashMap<Object, Object>();
        map.put("list",list);
        map.put("total",total);
        return Result.ok(null,map);
    }

    /**
     * 商家类型列表
     */
    @GetMapping("/select")
    public Result select(){

        RunfastBusinessTypeExample example=new RunfastBusinessTypeExample();
        example.createCriteria().andParentIdEqualTo(0);
        List<RunfastBusinessType> list = runfastBusinessTypeService.selectByExample(example);
        if(list.size()>0){
            for(RunfastBusinessType type:list){
                RunfastBusinessTypeExample example1=new RunfastBusinessTypeExample();
                example1.createCriteria().andParentIdEqualTo(type.getId());
                List<RunfastBusinessType> list1=runfastBusinessTypeService.selectByExample(example1);
                type.setSubList(list1);
            }
        }
        return Result.ok(null,list);
    }

    /**
     * 保存商家类型
     */
    @PostMapping("/save")
    public Result save(@RequestBody RunfastBusinessType type){
        if(type.getBroadHeading() !=null){
            type.setCreatetime(new Date());
            type.setName(type.getBroadHeading());
            type.setParentId(0);
            runfastBusinessTypeService.insertSelective(type);
       /* if(type.getSubclass()!=null){
            List<String> result = Arrays.asList(StringUtils.split(type.getSubclass(),","));
            for(String s:result){
                RunfastBusinessType type1=new RunfastBusinessType();
                type1.setName(s);
                type1.setCreatetime(new Date());
                type1.setParentId(type.getId());
                runfastBusinessTypeService.insertSelective(type1);
            }
         }*/
        if(type.getSubType().size()>0){
            for(Map<String,String> map:type.getSubType()){
                RunfastBusinessType type1=new RunfastBusinessType();
                type1.setName(map.get("name"));
                type1.setCreatetime(new Date());
                type1.setParentId(type.getId());
                runfastBusinessTypeService.insertSelective(type1);
            }

        }
        }else{ return Result.fail(null,"大类名称不能为空！");}
        return Result.ok(null);
    }

    /**
     * 修改商家类型
     */
    @PostMapping("/update")
    public Result update(@RequestBody RunfastBusinessType type){
        if(type==null){return Result.fail(null,"品类信息不能为空！");}
        if(type.getSubType().size()>0){
            RunfastBusinessTypeExample example=new RunfastBusinessTypeExample();
            example.createCriteria().andParentIdEqualTo(type.getId());
            List<RunfastBusinessType> subList=runfastBusinessTypeService.selectByExample(example);
            List<Integer> list=new ArrayList<Integer>();
            if(subList.size()>0){
                for(RunfastBusinessType type1:subList){
                    list.add(type1.getId());
                }
            }
            for(Map<String, String> sub:type.getSubType()){
                     System.out.println(sub.get(0));
                     if(StringUtils.isNotBlank(sub.get("id"))){
                         if(list.contains(StringUtil.stringToInteger( sub.get("id")))){
                             RunfastBusinessType businessType=new RunfastBusinessType();
                             businessType.setId(StringUtil.stringToInteger( sub.get("id")));
                             businessType.setName(sub.get("name"));
                             runfastBusinessTypeService.updateByPrimaryKeySelective(businessType);
                             RunfastBusinessExample example1=new RunfastBusinessExample();
                             example1.createCriteria().andMainClassIdEqualTo(businessType.getId());
                             List<RunfastBusiness> businessList=runfastBusinessService.selectByExample(example1);
                             if(businessList.size()>0){
                                 for(RunfastBusiness  business:businessList){
                                     business.setSalerange(businessType.getName());
                                     runfastBusinessService.updateByPrimaryKeySelective(business);
                                 }
                             }
                         }
                         list.remove(StringUtil.stringToInteger( sub.get("id")));
                     }else{
                         RunfastBusinessType businessType=new RunfastBusinessType();
                         businessType.setParentId(type.getId());
                         businessType.setName(sub.get("name"));
                         businessType.setCreatetime(new Date());
                         runfastBusinessTypeService.insertSelective(businessType);
                     }
            }
            if(list.size()>0){
                for(Integer l:list){
                    RunfastBusinessExample example1=new RunfastBusinessExample();
                    example1.createCriteria().andMainClassIdEqualTo(type.getId());
                    example1.or().andSubClassId1EqualTo(type.getId());
                    example1.or().andSubClassId2EqualTo(type.getId());
                    Integer i=runfastBusinessService.countByExample(example1);
                    if(i>0){
                        return Result.fail(null,"删除的品类已被应用，不能删除！");
                    }else{
                        runfastBusinessTypeService.deleteByPrimaryKey(l);
                    }
                }
            }
        }
        type.setName(type.getBroadHeading());
        runfastBusinessTypeService.updateByPrimaryKeySelective(type);
        return Result.ok(null);
    }

    /**
     * 删除商家分类
     */
    @PostMapping("/delete")
    public Result delete(@RequestBody RunfastBusinessType type){
        if(type==null){return Result.fail(null,"品类信息不能为空！");}
        RunfastBusinessType t=runfastBusinessTypeService.selectByPrimaryKey(type.getId());
        if(t!=null){
            if(t.getParentId()==0){
                RunfastBusinessTypeExample example=new RunfastBusinessTypeExample();
                example.createCriteria().andParentIdEqualTo(t.getId());
                List<RunfastBusinessType> list=runfastBusinessTypeService.selectByExample(example);
                if(list.size()>0){
                    for(RunfastBusinessType l:list){
                        RunfastBusinessExample example1=new RunfastBusinessExample();
                        example1.createCriteria().andMainClassIdEqualTo(l.getId());
                        example1.or().andSubClassId1EqualTo(l.getId());
                        example1.or().andSubClassId2EqualTo(l.getId());
                        Integer i=runfastBusinessService.countByExample(example1);
                        if(i>0){
                            return Result.fail(null,"删除的品类已被应用，不能删除！");
                        }else{
                            runfastBusinessTypeService.deleteByPrimaryKey(l.getId());
                        }
                    }
                }
                RunfastBusinessExample example2=new RunfastBusinessExample();
                example2.createCriteria().andMainClassIdEqualTo(type.getId());
                example2.or().andSubClassId1EqualTo(type.getId());
                example2.or().andSubClassId2EqualTo(type.getId());
                Integer i=runfastBusinessService.countByExample(example2);
                if(i>0){
                    return Result.fail(null,"删除的品类已被应用，不能删除！");
                }else{
                    runfastBusinessTypeService.deleteByPrimaryKey(type.getId());
                }
            }else{
                    RunfastBusinessExample example=new RunfastBusinessExample();
                    example.createCriteria().andMainClassIdEqualTo(type.getId());
                    example.or().andSubClassId1EqualTo(type.getId());
                    example.or().andSubClassId2EqualTo(type.getId());
                    Integer i=runfastBusinessService.countByExample(example);
                    if(i>0){
                       return Result.fail(null,"删除的品类已被应用，不能删除！");
                    }else{
                        runfastBusinessTypeService.deleteByPrimaryKey(type.getId());
                    }
            }
        }else{return Result.fail(null,"未找到对应的品类信息！");}
        return Result.ok(null);
    }



}
